import Plugin from '../../../lib/plugins/plugin.js';

export class ZhihuHotSearch extends Plugin {
  constructor() {
    super({
      name: '知乎热搜',
      dsc: '获取知乎热搜前十',
      event: 'message',
      priority: 5000,
      rule: [
        {
          reg: `^#(.*)(知乎热搜|zhihu热搜|/知乎热搜|/zhihu热搜)(.*)`,
          fnc: 'getZhihuHotSearch'
        },
      ]
    });
  }

  async getZhihuHotSearch(e) {
    try {
      const res = await fetch('https://www.haotechs.cn/ljh-wx/api/zhihuHot'); // 替换为正确的接口地址
      if (!res.ok) {
        throw new Error(`HTTP error! Status: ${res.status}`);
      }
      const jsonResponse = await res.json();
      if (jsonResponse && jsonResponse.code === 0 && jsonResponse.result && jsonResponse.result.top_search && Array.isArray(jsonResponse.result.top_search.words)) {
        const hotSearchList = jsonResponse.result.top_search.words.slice(0, 10);
        let replyText = '知乎热搜前十：\n';
        hotSearchList.forEach((item, index) => {
          replyText += `${index + 1}. ${item.display_query}\n`;
        });
        await this.reply(replyText);
      } else {
        await this.reply('未获取到知乎热搜数据，请检查接口是否正确');
      }
    } catch (err) {
      console.error('[知乎热搜] 接口请求失败或解析错误:', err);
      await this.reply('知乎热搜接口请求失败或解析错误，请检查链接的合法性，或稍后重试。');
    }
  }
}